from django.shortcuts import render, redirect
from .models import *

from django.core.paginator import Paginator,Page

# Create your views here.

# 商品页,也是网站首页
def index(request):

    #首页需要的数据
    # 商品类  类下面的4个商品
    #找到所有品类
    types = TypeInfo.objects.all()
    goods = []
    for t in types:
        dict = {'type':t}
        #找到每个品类下面的4个最新商品
        news = t.goodsinfo_set.all().order_by('-id')[0:3]
        #找到每个品类下面3个最热商品
        hots = GoodsInfo.objects.filter(gtype=t).order_by('-gclick')[0:4]
        dict['news'] = news
        dict['hots'] = hots
        goods.append(dict)

    #传过去的goods为:[{},{},{},{}]
    print(goods)
    context = {'title':'天天生鲜:首页','topcar':2,'goods':goods}
    return render(request, 'goods/index.html',context)


#列表页
def list(request,pageindex,typeid,sortid):
    # /page/typeid/sortid/
    #需要的数据
    #两个类下面的新品,其余都是类下面的产品,一页显示10个
    typeid = int(typeid)

    #价格/人气排行.这里又用个参数,1,代表常规,2代表价格升,3代表价格降,4代表人气
    sortid = int(sortid)
    if sortid == 1:
        sort = 'id'
    elif sortid == 2:
        sort = 'gprice'
    elif sortid ==3:
        sort= '-gprice'
    else:
        sort='-gclick'




    # todo 这里用get会报错,当id不存在的时候
    type = TypeInfo.objects.get(id=typeid)
    goods = GoodsInfo.objects.filter(gtype=type).order_by(sort)
    news = goods.order_by('-id')[0:2]


    goodslist = Paginator(goods, 3)
    print(goodslist)

    #对pageindex进行判断
    pageindex = int(pageindex)

    if pageindex<1:
        pageindex = 1
    elif pageindex > goodslist.num_pages:
        pageindex = goodslist.num_pages

    #总共显示5页
    # {1,2,3,4,5}
    # {4,5,6,7,8}
    #当pageindex为1,2的时候,加后面的3,4,5
    # 当为6的时候,上面减2,下面加2

    #控制页面
    print(pageindex,'---------------------------')

    # pageindex = 1,2,.....goodslist.num_pages;

    if goodslist.num_pages < 5:
        pagenumberlist = range(1,goodslist.num_pages+1)
    elif pageindex in [1,2]:
        pagenumberlist = range(1,6)
    elif pageindex in [goodslist.num_pages-1,goodslist.num_pages]:
        pagenumberlist = range(goodslist.num_pages-4,goodslist.num_pages+1)
    else:
        pagenumberlist = range(pageindex-2, pageindex + 3)


    #查询第一页的
    goodspage = goodslist.page(pageindex)
    print(goodspage)
    print(type)




    context={'title':'天天生鲜:列表页','topcar':2,
             'good':goodspage,'type':type,
             'news':news,'sortid':sortid,
             'pagenumberlist':pagenumberlist,}
    return render(request,'goods/list.html',context)


# 单个商品详情页
# url:127.0.0.1:8000/123/  最后一个就是gid,商品id
def detail(request,gid):

    #根据gid查询到该商品
    goodlist = GoodsInfo.objects.filter(id=gid)
    #如果查询到商品,则将这个返回给详情页
    # 没有,则跳转到首页
    if len(goodlist):
        good = goodlist[0]

        #类型
        type = good.gtype

        #新品
        goods = GoodsInfo.objects.filter(gtype=type).order_by('-id')
        news = goods.order_by('-id')[0:2]

        #将浏览记录存入cookie



        context = {'title':'商品详情页','topcar':2,'good':good,
                   'type':type,'news':news}
        response = render(request, 'goods/detail.html', context)

        # 将浏览记录存入cookie,键值对只能存储字符串
        histroy = request.COOKIES.get('history','')  # 'id,id,'
        # histroy += histroy.
        # ''.split(',')
        if histroy:
            histroy_list = histroy.split('&')
            print(histroy_list)
            while str(good.id) in histroy_list:
                histroy_list.remove(str(good.id))
            while len(histroy_list)>4:
                histroy_list.pop()
        else:
            histroy_list = []
        #如果增加到最后,那删除的时候就直接删了最后一个了
        histroy_list.insert(0,str(good.id))
        histroy_str = "&".join(histroy_list)     #30&29&1&30&93
        response.set_cookie('history', histroy_str)

    else:
        response = redirect('/')
    return response



